

from django.http import HttpResponse
from django.shortcuts import render, redirect

# Create your views here.

from django.shortcuts import render
from lists.models import Item
from lists.models import List

def home_page(request):
    # 把{{}}里面的属性new_item_text 喧嚷成POST属性item_text中的字符串
    return render(request, 'home.html')


def view_list(request, list_id):
    list_ = List.objects.get(id=list_id)
    # add_item 没有进入
    # print('\nview_list ', list_id)
    return render(request, 'list.html', {'list': list_})
    pass


def new_list(request):
    # POST请求来我这里
    list_ = List.objects.create()
    Item.objects.create(text=request.POST['item_text'], list=list_)
    return redirect('/lists/%d/' % (list_.id,))


def add_item(request, list_id):
    # print('Hello world')
    list_ = List.objects.get(id=list_id)
    Item.objects.create(text=request.POST['item_text'], list=list_)
    # return HttpResponse(content='Hello world')
    # 重定向回去
    return redirect('/lists/%d/' % (list_.id, ))
    pass


